package club.baimahu.pencil.servlet;

import club.baimahu.pencil.servlet.annotation.AnnotationScanner;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class SimpleHttpServlet implements HttpServlet{

    Map<String,Map<String,Handler>> maps = null;

    private String scanPackages = null;


    public SimpleHttpServlet(String scanPackages){

        if(scanPackages == null){
            throw new IllegalArgumentException("scanPackages can not be null");
        }
        Properties properties = new Properties();
        properties.put("scan-package", scanPackages);
        this.initConfig(properties);
    }


    @Override
    public void initConfig(Properties properties) {
        String packagePath = properties.getProperty("scan-package");
        maps = AnnotationScanner.getHandler(packagePath);
    }


    @Override
    public Handler getHandler(HttpServletRequest httpServletRequest) {

        String requestMethod = httpServletRequest.getFullHttpRequest().method().name();
        String uri = httpServletRequest.getUri();
        Map<String,Handler> mapHandler = maps.get(uri);
        if(mapHandler == null){
            return null;
        }
        return mapHandler.get(requestMethod.toUpperCase());

    }


}
